/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import database.ProductDAO;
import java.util.List;
import java.util.Map;
import model.Category;
import model.Product;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/")

public class HomeAction extends ActionSupport {
    
    private String title;
    private String pageDescription;
    private List<Product> productsResultList;
    private ProductDAO productDAO = new ProductDAO();
    
    public HomeAction() {
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPageDescription() {
        return pageDescription;
    }

    public void setPageDescription(String pageDescription) {
        this.pageDescription = pageDescription;
    }

    public List<Product> getProductsResultList() {
        return productsResultList;
    }

    public void setProductsResultList(List<Product> productsResultList) {
        this.productsResultList = productsResultList;
    }
    
    @Override
    public String execute() throws Exception {
        productsResultList = productDAO.getNewestProducts();
        
        return SUCCESS;
    }
    
    @Action(value = "home", results = {
        @Result(name = "success", location = "/home.jsp")
    })
    public String home(){
        productsResultList = productDAO.getNewestProducts();
        
        Map session = ActionContext.getContext().getSession();
        List<Category> categories = productDAO.getAllCategories();
        session.put("categories", categories);
        
        title = "Home - Newest Products";
        pageDescription = "Newest Products on Online Shopping System";

        return SUCCESS;
    }
    
    @Action(value = "best-selling", results = {
        @Result(name = "success", location = "/home.jsp")
    })
    public String bestSelling(){
        productsResultList = productDAO.getBestSellingProducts();
        
        title = "Best Selling Products";
        pageDescription = "Best Selling Products on Online Shopping System";
        
        return SUCCESS;
    }
    
    @Action(value = "sale", results = {
        @Result(name = "success", location = "/home.jsp")
    })
    public String sale(){
        productsResultList = productDAO.getSaleProducts();
        
        title = "Hot Sale Products";
        pageDescription = "Hot Sale Products on Online Shopping System";
        
        return SUCCESS;
    }
}